﻿Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.OleDb
Imports ADOEntity
Imports ADOEntityImportData
Imports System.Net

Partial Class module_index
    Inherits System.Web.UI.Page

    Dim ds As DataSet
    Dim da As SqlDataAdapter
    Dim clsEdmUser As New clsEdmUser
    Dim datePwdExpire As Date
    Dim intIsLock, intIsInactive As Integer


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim strStatusSession As String = Request.QueryString("SessionOut")
            If Not (strStatusSession Is Nothing) Then
                ScriptManager.RegisterStartupScript(Me, _
                                                                 Me.GetType(), _
                                                                "clientScript", _
                                                                "alert('เนื่องจากไม่มีการใช้งานเกิน 20 นาที กรุณา longin ใหม่');", _
                                                                True)
            End If

            'ds = New DataSet()
            lblErrorMsg.Visible = False
            LoadInfoImage()
        Else
            'ds = Session("ds")
        End If
        ' Dns.
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        'Session("ds") = ds
    End Sub

    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Dim strIpLogin As String = ""
        Dim strComName As String = ""
        strIpLogin = Request.UserHostAddress
        strComName = Request.UserHostName



        Dim clsMenuRight As New clsSessionMenuRight

        clsMenuRight = clsEdmUser.checkUser(txtUsername.Text.Trim, _
                                            txtPassword.Text.Trim, strIpLogin, strComName)

        If Not clsMenuRight.getUserName = "" Then
            lblErrorMsg.Visible = False
            Session("ClsMenuRight") = clsMenuRight

            If (clsEdmUser.chkMustChangePwd = True) Then
                If Not (clsEdmUser.Msg = "") Then
                    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                                    String.Format("alert('{0}');location.href = 'changePassword.aspx';", clsEdmUser.Msg), True)
                Else
                    Response.Redirect("changePassword.aspx")
                End If


            Else

                If (clsEdmUser.Msg = "") Then
                    'ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                    '                                "alert('" + clsEdmUser.Msg.Replace("'", "") + "');location.href = 'News.aspx';", True)
                    Response.Redirect("News.aspx")
                Else
                    If (clsMenuRight.getIsPwdExpire = 1) Then
                        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                                  String.Format("alert('{0}');location.href = 'changePassword.aspx';", clsEdmUser.Msg.Replace("'", "")), True)
                    Else
                        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                                    String.Format("alert('{0}');location.href = 'News.aspx';", clsEdmUser.Msg.Replace("'", "")), True)
                        ' Response.Redirect("News.aspx")
                    End If


                End If

            End If

        Else
            If clsEdmUser.chkIsPwdExpire Then
                ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                                  String.Format("alert('{0}');location.href = 'changePassword2.aspx?user={1}';", clsEdmUser.Msg.Replace("'", ""), txtUsername.Text.Trim), True)
            
            ElseIf Not (clsEdmUser.ErrExp = "") Then
                'ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                '                                   "alert('" + clsEdmUser.ErrExp.Replace("'", "") + "');", True)
                ScriptManager.RegisterStartupScript(Me, _
                                                                  Me.GetType(), _
                                                                 "clientScript", _
                                                                 String.Format("alert('{0}');", clsEdmUser.ErrExp.Replace("'", "")), _
                                                                 True)
            Else
                If Not (clsEdmUser.Msg = "") Then

                    'If clsEdmUser.Msg = "isPwdExp" Then
                    '    'ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                    '    '"alert('รหัสผ่านหมดอายุ กรุณาเปลี่ยนรหัสผ่าน');location.href = 'changePassword.aspx?PwdExp=1';", True)
                    '    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                    '                              "alert('" + clsEdmUser.Msg + "');location.href = 'changePassword.aspx';", True)
                    'Else
                    ScriptManager.RegisterStartupScript(Me, _
                                                                                      Me.GetType(), _
                                                                                     "clientScript", _
                                                                                     String.Format("alert('{0}');", clsEdmUser.Msg.Replace("'", "")), _
                                                                                     True)
                    'End If


                End If

            End If

            'lblErrorMsg.Visible = True
            'txtPassword.Text = ""
        End If

    End Sub

    Public Sub WriteError(ByVal objEx As Exception, ByVal strFunctionName As String, ByVal strFromName As String)
        Try
            'Dim clsSessionMenuRight As clsSessionMenuRight = Session("clsMenuRight")
            Dim cls As New clsWriteError
            cls.GetstrErrorMessage = objEx.Message
            cls.GetstrErrorException = If(objEx.InnerException Is Nothing, "", objEx.InnerException.Message)
            cls.GetstrFromName = strFromName
            cls.GetstrUserName = txtUsername.Text
            cls.GetstrUserRight = "all"
            cls.GetstrFunctionName = strFunctionName
            cls.CreateFileXml(txtUsername.Text)
            'clsSessionMenuRight = Nothing
            cls = Nothing
        Catch ex As Exception

        End Try

    End Sub


    Protected Sub btncancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncancel.Click
        txtUsername.Text = ""
        txtPassword.Text = ""
    End Sub

    Private Sub LoadInfoImage()
        Try
            Dim importDataEntity = New ImportDataEntity
            Dim ImageInfoList = (From loginInfo In importDataEntity.KYC_LOGIN_INFO Where loginInfo.ACTIVE_DATE <= Date.Now And (loginInfo.EXPIRE_DATE Is Nothing Or loginInfo.EXPIRE_DATE >= Date.Now) Order By loginInfo.ACTIVE_DATE Descending Select loginInfo.FILE_NAME, loginInfo.INFO_NAME).ToList

            If ImageInfoList.Count > 0 Then
                Dim fileName As String = ImageInfoList.Item(0).FILE_NAME
                imgInfo.ImageUrl = System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings("Path_KYC_LOGIN_INFO"), fileName)
                imgInfo.AlternateText = ImageInfoList.Item(0).INFO_NAME
                imgInfo.Visible = True
            End If
        Catch
        End Try
    End Sub


    Protected Sub btnChangePwd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChangePwd.Click
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                                  String.Format("location.href = 'changePassword2.aspx?user={0}';", txtUsername.Text), True)
    End Sub

End Class
